package com.util.message;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;

import com.core.template.BaseData;
import com.core.template.Industry;
import com.core.template.TempData;
import com.core.template.TempMsg;
import com.util.web.Access;
import com.wechat.admin.model.Address;
import com.wechat.admin.model.Order;

public class TemplateMessage {
	//post请求
	private static final String SET_INDUSTRY_URL = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN";
	private static final String GET_TEMPLATEID_URL = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN";
	private static final String SEND_TEMPLATEIDMSG_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
	//get请求
	private static final String GET_INDUSTRY_URL = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=ACCESS_TOKEN";
	private static final String GET_TEMPLATELIST_URL = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN";
	//模板id
//	private static final String ID_AddOrderTemp = "sbbTJS_58NRQwI1eKA5_dqm12sf-YxCqUhkjKlDZtgc";
//	private static final String ID_AddOrderTemp = "gZ4K6Z8_7zQIIHzQXaBsRH6yXH4sHbg7x7PjM583QBE";
//	private static final String ID_AddOrderTemp = "mJWaIfGxMgQiYRT5s0IIgORyBhBVcL3X8sIpZgCsn0I";
	//自定义的模板消息
//	private static final String ID_AddOrderTemp = "UHu-5mMqDjmfu192_OGzY5tZRjftCVu4rNFyWo8ALv8";
	//较完整的订单模板消息
	private static final String ID_AddOrderTemp = "EpNFut-pq0TpG3n8yEvTavH_HDLbyYI0BXlRhsNWZh0";
	
	public static void setIndustryId() throws Exception{
		String url = SET_INDUSTRY_URL.replace("ACCESS_TOKEN", Access.getAccessToken()); 
		Industry industry = new Industry();
		industry.setIndustry_id1("13");
		industry.setIndustry_id2("1");
		Access.doPostStr(url, new JSONObject(industry).toString());
		System.out.println(Access.doGetStr(GET_INDUSTRY_URL.replace("ACCESS_TOKEN", Access.getAccessToken())));
	}
	
	public static void getTemplateId() throws Exception{
		String url = GET_TEMPLATEID_URL.replace("ACCESS_TOKEN", Access.getAccessToken());
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("template_id_short", "TM00015");
		System.out.println(Access.doPostStr(url, jsonObject.toString()));
	}
	
	public static void getTemplateList() throws Exception{
		String url = GET_TEMPLATELIST_URL.replace("ACCESS_TOKEN", Access.getAccessToken());
		System.out.println(Access.doGetStr(url));
	}
	
	public static void sendTemplateMsg(String toUser, Address addr, Order order, String notes) throws Exception{
		String url = SEND_TEMPLATEIDMSG_URL.replace("ACCESS_TOKEN", Access.getAccessToken());
		TempMsg tempMsg = new TempMsg();
		
		TempData tempData = new TempData();
		BaseData []baseData = new BaseData[8];
		
		for (int i=0;i<baseData.length;i++) {
			baseData[i] = new BaseData();
			if(i<baseData.length-1){
				baseData[i].setColor("#173177");
			}
		}
		
		baseData[0].setValue("恭喜您的订单已成功下发");
		tempData.setFirst(baseData[0]);
		
		baseData[1].setValue(addr.getName());
		tempData.setReceiver(baseData[1]);
		
		baseData[2].setValue(addr.getAddr());
		tempData.setAddr(baseData[2]);
		
		baseData[3].setValue(addr.getPhone());
		tempData.setPhone(baseData[3]);
		
		baseData[4].setValue(order.getCompany());
		tempData.setCompany(baseData[4]);
		
		baseData[5].setValue(order.getNumber());
		tempData.setNumber(baseData[5]);
		
		baseData[6].setValue("模板消息测试");
		if(!StringUtils.isEmpty(notes)){
			baseData[6].setValue(notes);
		}
		tempData.setNotes(baseData[6]);

		baseData[7].setValue("感谢您的使用，订单多多下~~");
		tempData.setRemark(baseData[7]);
		
		tempMsg.setTouser(toUser);
		tempMsg.setTemplate_id(ID_AddOrderTemp);
		tempMsg.setUrl("http://weixin.qq.com/download");
		tempMsg.setData(tempData);
		
		System.out.println(Access.doPostStr(url, new JSONObject(tempMsg).toString()));
	}
	
	public static void main(String[] args) throws Exception {
//		setIndustryId();
//		getTemplateList();
//		getTemplateId();
//		sendTemplateMsg("oH7MhweqUwhYOnBsGuPGRafK-Hwg", "三峡大学欣苑学生公寓", "京东快递129349345910", "");
	}
	
}
